package com.tengju.bff.interfaces.app.activity.facade;

import com.tengju.activity.application.ActivityService;
import com.tengju.activity.domain.model.activity.ActivityCode;
import com.tengju.activity.domain.model.activity.ActivityConfigRepository;
import com.tengju.activity.domain.model.activity.ActivityId;
import com.tengju.activity.domain.model.activity.SaleSituation;
import com.tengju.activity.domain.service.SaleSituationService;
import com.tengju.bff.interfaces.app.activity.vo.SaleSituation66VO;
import com.tengju.bff.interfaces.shared.AmountUtil;
import com.tengju.bff.interfaces.shared.Formatter;
import com.tengju.bff.interfaces.shared.InterfaceCode;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.data.domain.model.exchvesion.DataExchVersion;
import com.tengju.data.domain.model.exchvesion.DataExchVersionRepository;
import com.tengju.data.domain.model.exchvesion.DataName;
import com.tengju.user.domain.model.user.UserIdCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Optional;


@Service
public class ActivityFacade {

    @Value("${abm.download.url}")
    private String abmDownloadUrl;

    @Value("${abm.66.jump.url}")
    private String abmJumpUrl;

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityConfigRepository activityConfigRepository;
    @Autowired
    private SaleSituationService saleSituationService;
    @Autowired
    private DataExchVersionRepository dataExchVersionRepository;

    public SaleSituation66VO saleSituation66(UserIdCode idCode) {

        //获取活动id
        ActivityId activityId = activityConfigRepository.queryActivityId(ActivityCode.ACTIVITY_66_2021.getCode());

        if (activityId == null) {
            throw new InterfaceException(InterfaceCode.ACTIVITY_CONFIG_ERROR);
        }

        Boolean hasSignUp = activityService
                .hasSignUp(new com.tengju.activity.domain.model.user.UserIdCode(idCode.getValue()), activityId);

        //获取数据最新版本号
        DataExchVersion dataExchVersion = dataExchVersionRepository.queryVersion(DataName.REPORT_ACTIVITY_SALE_MONEY);


        SaleSituation saleSituation = saleSituationService.getSaleSituation(
                new com.tengju.activity.domain.model.user.UserIdCode(idCode.getValue()),
                activityId, Optional.ofNullable(dataExchVersion).map(DataExchVersion::getVersion).orElse(0));

        return new SaleSituation66VO(
                activityId.getValue(),
                hasSignUp,
                Optional.ofNullable(saleSituation).map(SaleSituation::getSaleTarget).map(AmountUtil::parseToThousandByInt).orElse("0"),
                Optional.ofNullable(saleSituation).map(SaleSituation::getSales).map(AmountUtil::parseToThousandByDouble).orElse("0.00"),
                Optional.ofNullable(saleSituation).map(SaleSituation::completionRate).orElse("0.00%"),
                Optional.ofNullable(saleSituation).map(SaleSituation::getCompletionRate).map(BigDecimal::doubleValue).orElse(0.00),
                abmJumpUrl,
                abmDownloadUrl
        );
    }


}
